package com.shs.bus.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.shs.bus.entity.Goods;
import com.shs.bus.entity.Inport;
import com.shs.bus.entity.Provider;
import com.shs.bus.service.GoodsService;
import com.shs.bus.service.InportService;
import com.shs.bus.service.ProviderService;
import com.shs.bus.vo.GoodsVo;
import com.shs.bus.vo.InportVo;
import com.shs.sys.common.*;
import com.shs.sys.entity.User;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.List;

/**
 * <p>
 *  进货管理前端控制器
 * </p>
 *
 * @author jxm
 * @since 2020-04-17
 */
@RestController
@RequestMapping("inport")
public class InportController {

    @Autowired
    private InportService inportService;
    @Autowired
    private ProviderService providerService;
    @Autowired
    private GoodsService goodsService;

    /**
     * 查询进货商品
     * @return
     */
    @RequestMapping("loadAllInport")
    public DataGridView loadAllInport(InportVo vo){
        Page<Inport> page = new Page<>(vo.getPage(),vo.getLimit());
        QueryWrapper<Inport> wrapper = new QueryWrapper();
        wrapper.eq(vo.getProviderid()!=null && vo.getProviderid()!=0,"providerid",vo.getProviderid());
        wrapper.eq(vo.getGoodsid()!=null && vo.getGoodsid()!=0,"goodsid",vo.getGoodsid());
        wrapper.like(StringUtils.isNotBlank(vo.getOperateperson()),"operateperson",vo.getOperateperson());
        wrapper.ge(vo.getStartTime()!=null,"inporttime",vo.getStartTime());
        wrapper.le(vo.getEndTime()!=null,"inporttime",vo.getEndTime());
        wrapper.orderByDesc("inporttime");
        inportService.page(page,wrapper);
        List<Inport> records = page.getRecords();
        for (Inport record : records) {
            Provider provider = providerService.getById(record.getProviderid());
            if(null!=provider){
                record.setProvidername(provider.getProvidername());
            }
            Goods goods = goodsService.getById(record.getGoodsid());
            if(null!=goods){
                record.setGoodsname(goods.getGoodsname());
                record.setSize(goods.getSize());
            }
        }
        return new DataGridView(page.getTotal(),records);
    }

    /**
     * 添加进货商品
     * @param vo
     * @return
     */
    @RequestMapping("addInport")
    public ResultObj addInport(InportVo vo){
        try {
            vo.setInporttime(new Date());
            User user = (User) WebUtils.getSession().getAttribute("user");
            vo.setOperateperson(user.getName());
            inportService.save(vo);
            return ResultObj.ADD_SUCCESS;
        } catch (Exception e) {
            e.printStackTrace();
            return ResultObj.ADD_ERROR;
        }
    }

    /**
     * 修改进货商品
     * @param vo
     * @return
     */
    @RequestMapping("updateInport")
    public ResultObj updateInport(InportVo vo){
        try {
            inportService.updateById(vo);
            return ResultObj.UPDATE_SUCCESS;
        } catch (Exception e) {
            e.printStackTrace();
            return ResultObj.UPDATE_ERROR;
        }
    }

    /**
     * 删除
     * @param id
     * @return
     */
    @RequestMapping("deleteInport")
    public ResultObj deleteInport(Integer id){
        try {
            inportService.removeById(id);
            return ResultObj.DELETE_SUCCESS;
        } catch (Exception e) {
            e.printStackTrace();
            return ResultObj.DELETE_ERROR;
        }
    }
}
